package com.example.hrandroidapp.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.hrandroidapp.R;
import com.example.hrandroidapp.common.Constant;
import com.example.hrandroidapp.common.DateUtils;
import com.example.hrandroidapp.common.HRLog;
import com.example.hrandroidapp.components.CircleButton;
import com.example.hrandroidapp.domain.OverTimePO;

public class OvertimeApprovalAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	
	private Context context;
	private List<OverTimePO> lstOvertime;
	private boolean checkable=true;
	private int count=0;
	
	private AcceptCallBack callback;

	public OvertimeApprovalAdapter(Context context, List<OverTimePO> lstOvertime, AcceptCallBack callback) {
		this.context=context;
		this.lstOvertime=lstOvertime;
		this.callback=callback;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return lstOvertime.size();
	}

	@Override
	public Object getItem(int position) {
		return lstOvertime.get(position);
	}

	public List<OverTimePO> getAllItems() {
		return lstOvertime;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.leave_approval_item, null);

        }
		
		final CircleButton circleBtn = (CircleButton) convertView.findViewById(R.id.img_badge);
		circleBtn.setTag(lstOvertime.get(position).isCheck());
		
		final TextView tvMailCircleTxt = (TextView) convertView.findViewById(R.id.tv_mail_circle_text);
		HRLog.postLogInfo("circleBtn tag: " + lstOvertime.get(position).isCheck() + " position: " + position, " at LeaveApprovalAdapter.getView"); 
		if(lstOvertime.get(position).isCheck()) {
			circleBtn.setButtonColor(Color.GRAY);
//			circleBtn.setShadowColor(Color.BLACK);
			circleBtn.setImageResource(R.drawable.checkmark_light_l);
        	tvMailCircleTxt.setText("");
		}else {
			circleBtn.setButtonColor(Constant.APP_BLUE_COLOR);
//			circleBtn.setShadowColor(Color.BLACK);
			circleBtn.setImageResource(0);
			tvMailCircleTxt.setText(lstOvertime.get(position).getEmployeeName().substring(0, 1).toUpperCase());
		}
		
		TextView tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
        TextView tvTimeStamp = (TextView) convertView.findViewById(R.id.tv_timestamp);
        TextView tvBody = (TextView) convertView.findViewById(R.id.tv_body);
        
		tvTitle.setText(lstOvertime.get(position).getEmployeeName());
		tvTimeStamp.setText(DateUtils.convert(lstOvertime.get(position).getOtDate(), DateUtils.DATE_MMMDD_FORMAT));
		tvBody.setText(lstOvertime.get(position).getTask());
        
		circleBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(checkable) {
				int iColor=0;
					if (lstOvertime.get(position).isCheck()) {
						
						((CircleButton)v).setButtonColor(Constant.APP_BLUE_COLOR);
//							((CircleButton)v).setShadowColor(Constant.APP_BLUE_COLOR);
						iColor = Constant.APP_BLUE_COLOR;
						
						((CircleButton)v).setImageResource(0);
						tvMailCircleTxt.setText(lstOvertime.get(position).getEmployeeName().substring(0, 1).toUpperCase());

						((CircleButton)v).setButtonColor(iColor);
						((CircleButton)v).setShadowColor(iColor);
						
						v.setId(0);
						lstOvertime.get(position).setCheck(false);
						count--;
						HRLog.postLogInfo("count value uncheck: " + count, "LeaveApprovalAdapter.getView");
						callback.setAccptVisible((count > 0)? false: true);
	                } else {
	                	((CircleButton)v).setButtonColor(Color.GRAY);
//	                	((CircleButton)v).setShadowColor(Color.GRAY);
	                	((CircleButton)v).setImageResource(R.drawable.checkmark_light_l);
	                	tvMailCircleTxt.setText("");
	                	
//	                	v.setId(1);
						lstOvertime.get(position).setCheck(true); 
						count++;
						HRLog.postLogInfo("count value check: " + count, "LeaveApprovalAdapter.getView");
						callback.setAccptVisible((count > 0)? false: true);
	                }
				}
			}
		});
        
        return convertView;
	}

	public void toggleCircleButton(boolean flag) {}
	
	public void setCheckableListView(boolean checkable) {
		this.checkable=checkable;
	}

	public List<OverTimePO> getLeaves() {
		return lstOvertime;
	}
	
	public void setLeaves(List<OverTimePO> lstOvertime) {
		this.lstOvertime = lstOvertime;
		notifyDataSetChanged();
	}
	
	public void addOverTime(OverTimePO overtime) {
		lstOvertime.add(overtime);
		notifyDataSetChanged();
	}

	public void addOvertimes(List<OverTimePO> lstOvertime) {
		this.lstOvertime.addAll(lstOvertime);
		notifyDataSetChanged();
	}
	
	public void addOvertimes(List<OverTimePO> lstOvertime, int count) {
		addOvertimes(lstOvertime.subList(0, count));
	}
	
	public void setOvertime(int position, OverTimePO overtime) {
		lstOvertime.set(position, overtime);
		notifyDataSetChanged();
	}
	
	public void remove(int index) {
		lstOvertime.remove(index); 
		notifyDataSetChanged();
	}
	
	public void remove(OverTimePO overtime) {
		lstOvertime.remove(overtime);
		notifyDataSetChanged();
	}
	
	public void clear() {
		lstOvertime.clear();
		notifyDataSetChanged();
	}
	
	public static interface AcceptCallBack {
		void setAccptVisible(boolean flag);
	}

}
